package com.ctshk.rpc.scenic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.thirdparty.TicketApiUtil;
import com.ctshk.common.utils.thirdparty.airshowResp.ApiResult;
import com.ctshk.common.utils.thirdparty.airshowResp.TicketResp;
import com.ctshk.common.utils.thirdparty.airshowResp.TicketSceneResp;
import com.ctshk.rpc.scenic.dto.AirshowProductDTO;
import com.ctshk.rpc.scenic.dto.AirshowSceneDTO;
import com.ctshk.rpc.scenic.dto.AirshowTicketRespDTO;
import com.ctshk.rpc.scenic.entity.AirshowDistributorSetting;
import com.ctshk.rpc.scenic.entity.AirshowProduct;
import com.ctshk.rpc.scenic.mapper.AirshowDistributorSettingMapper;
import com.ctshk.rpc.scenic.mapper.AirshowProductMapper;
import com.ctshk.rpc.scenic.req.AirshowProductListReq;
import com.ctshk.rpc.scenic.service.IAirshowProductReserveService;
import com.ctshk.rpc.scenic.service.IAirshowProductService;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@DubboService
public class AirshowProductReserveServiceImpl extends ServiceImpl<AirshowProductMapper, AirshowProduct> implements IAirshowProductReserveService {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private AirshowProductMapper airshowProductMapper;

    @Autowired
    private AirshowDistributorSettingMapper airshowDistributorSettingMapper;


    @Override
    public PageResponse<AirshowProductDTO> getProductListByParams(AirshowProductListReq airshowProductReq, TokenUser tokenUser) {

        QueryWrapper<AirshowProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("product_name",airshowProductReq.getProductName());
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("is_enable",1);

        Page<AirshowProduct> iPage = new Page<>(airshowProductReq.getPageNo(), airshowProductReq.getPageSize());

        iPage = airshowProductMapper.selectPage(iPage,queryWrapper);

        List<AirshowProductDTO> airshowProductDTOList = EntityUtil.copyList(iPage.getRecords(), AirshowProductDTO.class);

        if(TicketApiUtil.isCTSHK(tokenUser.getId())){
            for(AirshowProductDTO airshowProductDTO : airshowProductDTOList){
                airshowProductDTO.setAgentPrice(airshowProductDTO.getSalePrice());
            }
        }

        PageResponse<AirshowProductDTO> result = new PageResponse<>(airshowProductDTOList, iPage.getCurrent() == 1,
                iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());

        return result;
    }

    @Override
    public Result<List<AirshowTicketRespDTO>> getTicketList(TokenUser tokenUser) {

        if(tokenUser == null){
            throw new BusinessException(SystemError.SYS_406);
        }

        List<AirshowTicketRespDTO> results = new ArrayList<>();

        Object cache = redisTemplate.opsForValue().get("AIR_SHOW_API_LIST_CACHE");

        if(cache != null){
            results = (List<AirshowTicketRespDTO>) cache;
        }else{
            ApiResult<List<TicketSceneResp>> listApiResult = TicketApiUtil.ticketScene(TicketApiUtil.SHOW_ID);

            if("10001".equals(listApiResult.getCode())){
                //查詢成功
                List<TicketSceneResp> ticketSceneResps = listApiResult.getData();

                for(TicketSceneResp ticketSceneResp : ticketSceneResps){
                    ApiResult<List<TicketResp>> ticket = TicketApiUtil.ticket(Long.valueOf(ticketSceneResp.getSceneId()));

                    if(!"10001".equals(ticket.getCode())){
                        continue;
                    }
                    for(TicketResp ticketResp : ticket.getData()){
                        AirshowTicketRespDTO airshowTicketRespDTO = new AirshowTicketRespDTO();
                        airshowTicketRespDTO.setSceneId(ticketSceneResp.getSceneId());
                        airshowTicketRespDTO.setSceneName(ticketSceneResp.getSceneName());

                        airshowTicketRespDTO.setTktId(ticketResp.getTktId());
                        airshowTicketRespDTO.setTicketName(ticketResp.getTicketName());
                        airshowTicketRespDTO.setTicketPrice(ticketResp.getTicketPrice());
                        airshowTicketRespDTO.setAvailableCeiling(ticketResp.getAvailableCeiling());
                        airshowTicketRespDTO.setPayCeiling(ticketResp.getPayCeiling());
                        airshowTicketRespDTO.setTicketType(ticketResp.getTicketType());
                        airshowTicketRespDTO.setAvailableWarning(ticketResp.getAvailableWarning());
                        airshowTicketRespDTO.setDiscount(ticketResp.isDiscount());
                        airshowTicketRespDTO.setEntranceCountLow(ticketResp.getEntranceCountLow());
                        airshowTicketRespDTO.setUseNumber(ticketResp.getUseNumber());
                        airshowTicketRespDTO.setShowId(ticketResp.getShowId());
                        airshowTicketRespDTO.setUseRules(ticketResp.getUseRules());
                        airshowTicketRespDTO.setUseRulesEn(ticketResp.getUseRulesEn());
                        airshowTicketRespDTO.setEntranceCountUp(ticketResp.getEntranceCountUp());
                        airshowTicketRespDTO.setOut(ticketResp.isOut());

                        results.add(airshowTicketRespDTO);
                    }

                }

                redisTemplate.opsForValue().set("AIR_SHOW_API_LIST_CACHE",results,1, TimeUnit.DAYS);


            }else
                throw new BusinessException(SystemError.SYS_433,listApiResult.getMessage());
        }


        AirshowDistributorSetting airshowDistributorSetting = airshowDistributorSettingMapper.selectById(tokenUser.getId());

        for(AirshowTicketRespDTO airshowTicketRespDTO : results){
            //设置该分销商的结算价

            if(TicketApiUtil.discountTktIds.contains(airshowTicketRespDTO.getTktId())){
                airshowTicketRespDTO.setTicketPrice(airshowDistributorSetting.getDiscountPrice().doubleValue());
            }else{
                airshowTicketRespDTO.setTicketPrice(airshowDistributorSetting.getNormalPrice().doubleValue());
            }

        }

        return Result.success(results);
    }

    @Override
    public Result<List<AirshowSceneDTO>> getTicketScene(TokenUser tokenUser) {

        if(tokenUser == null){
            throw new BusinessException(SystemError.SYS_406);
        }

        List<AirshowSceneDTO> list = new ArrayList<>();

        ApiResult<List<TicketSceneResp>> listApiResult = TicketApiUtil.ticketScene(TicketApiUtil.SHOW_ID);

        for(TicketSceneResp ticketSceneResp : listApiResult.getData()){
            AirshowSceneDTO airshowSceneDTO = new AirshowSceneDTO();
            airshowSceneDTO.setSceneId(ticketSceneResp.getSceneId());
            airshowSceneDTO.setSceneName(ticketSceneResp.getSceneName());

            list.add(airshowSceneDTO);
        }

        return Result.success(list);
    }

    @Override
    public Result<List<AirshowTicketRespDTO>> getTicketBySceneId(Long sceneId, TokenUser tokenUser) {

        if(tokenUser == null){
            throw new BusinessException(SystemError.SYS_406);
        }

        List<AirshowTicketRespDTO> results = new ArrayList<>();

        ApiResult<List<TicketResp>> ticket = TicketApiUtil.ticket(sceneId);

        if(!"10001".equals(ticket.getCode())){
            throw new BusinessException(SystemError.SYS_433,ticket.getMessage());
        }
        for(TicketResp ticketResp : ticket.getData()){

            AirshowTicketRespDTO airshowTicketRespDTO = new AirshowTicketRespDTO();
            airshowTicketRespDTO.setSceneId(ticketResp.getSceneId());

            airshowTicketRespDTO.setTktId(ticketResp.getTktId());
            airshowTicketRespDTO.setTicketName(ticketResp.getTicketName());
            airshowTicketRespDTO.setTicketPrice(ticketResp.getTicketPrice());
            airshowTicketRespDTO.setAvailableCeiling(ticketResp.getAvailableCeiling());
            airshowTicketRespDTO.setPayCeiling(ticketResp.getPayCeiling());
            airshowTicketRespDTO.setTicketType(ticketResp.getTicketType());
            airshowTicketRespDTO.setAvailableWarning(ticketResp.getAvailableWarning());
            airshowTicketRespDTO.setDiscount(ticketResp.isDiscount());
            airshowTicketRespDTO.setEntranceCountLow(ticketResp.getEntranceCountLow());
            airshowTicketRespDTO.setUseNumber(ticketResp.getUseNumber());
            airshowTicketRespDTO.setShowId(ticketResp.getShowId());
            airshowTicketRespDTO.setUseRules(ticketResp.getUseRules());
            airshowTicketRespDTO.setUseRulesEn(ticketResp.getUseRulesEn());
            airshowTicketRespDTO.setEntranceCountUp(ticketResp.getEntranceCountUp());
            airshowTicketRespDTO.setOut(ticketResp.isOut());

            results.add(airshowTicketRespDTO);
        }

        AirshowDistributorSetting airshowDistributorSetting = airshowDistributorSettingMapper.selectById(tokenUser.getId());

        for(AirshowTicketRespDTO airshowTicketRespDTO : results){
            //设置该分销商的结算价

            if(TicketApiUtil.discountTktIds.contains(airshowTicketRespDTO.getTktId())){
                airshowTicketRespDTO.setTicketPrice(airshowDistributorSetting.getDiscountPrice().doubleValue());
            }else{
                airshowTicketRespDTO.setTicketPrice(airshowDistributorSetting.getNormalPrice().doubleValue());
            }

        }

        return Result.success(results);
    }
}
